package cn.roco.quicksearch.util;

import java.lang.reflect.Field;
import java.util.*;

import com.badibadi.abcpinying.SortModel;
import com.badibadi.activity.ChatActivity;
import com.badibadi.mytools.Constants;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchAdapter<T> extends BaseAdapter implements Filterable {
	private List<T> mObjects;

	private List<Set<String>> pinyinList;// 支持多音字,类似:{{z,c},{j},{z},{q,x}}的集合

	private final Object mLock = new Object();

	private int mResource;

	private int mFieldId = 0;

	private Context mContext;

	private ArrayList<T> mOriginalValues;
	private ArrayFilter mFilter;

	private LayoutInflater mInflater;

	public static final int ALL = -1;// 全部
	private int maxMatch = 20;// 最多显示多少个可能选项

	private DisplayImageOptions options;

	/**
	 * 支持多音字
	 */
	public SearchAdapter(Context context, int textViewResourceId, T[] objects,
			int maxMatch) {
		// TODO Auto-generated constructor stub
		init(context, textViewResourceId, 0, Arrays.asList(objects));
		this.pinyinList = getHanziSpellList(objects);
		this.maxMatch = maxMatch;
	}

	public SearchAdapter(Context context, int textViewResourceId,
			List<T> objects, int maxMatch) {
		// TODO Auto-generated constructor stub
		init(context, textViewResourceId, 0, objects);
		this.pinyinList = getHanziSpellList(objects);
		this.maxMatch = maxMatch;
		//    
	}

	private void init(Context context, int resource, int textViewResourceId,
			List<T> objects) {
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mResource = resource;
		mObjects = objects;
		mFieldId = textViewResourceId;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
//		   
	}

	/**
	 * 获得汉字拼音首字母列表
	 */
	private List<Set<String>> getHanziSpellList(T[] hanzi) {
		List<Set<String>> listSet = new ArrayList<Set<String>>();
		PinYin4j pinyin = new PinYin4j();
		for (int i = 0; i < hanzi.length; i++) {
			listSet.add(pinyin.getPinyin(hanzi[i].toString()));
		}
		return listSet;
	}

	/**
	 * 获得汉字拼音首字母列表
	 */
	private List<Set<String>> getHanziSpellList(List<T> hanzi) {
		List<Set<String>> listSet = new ArrayList<Set<String>>();
		PinYin4j pinyin = new PinYin4j();
		for (int i = 0; i < hanzi.size(); i++) {
//			   
			listSet.add(pinyin.getPinyin(((SortModel) hanzi.get(i))
					.getNickName()));
		}
		return listSet;
	}

	public int getCount() {
		return mObjects.size();
	}

	public T getItem(int position) {
		return mObjects.get(position);
	}

	public int getPosition(T item) {
		return mObjects.indexOf(item);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		return createViewFromResource(position, convertView, parent, mResource);
	}

	private View createViewFromResource(final int position, View convertView,
			ViewGroup parent, int resource) {
		View view;
		//    
		// TextView text;
		ImageView paoPaoButton1;
		ImageView child_is_login;
		TextView child_name;
		TextView child_qianming;

		if (convertView == null) {
			view = mInflater.inflate(resource, parent, false);
		} else {
			view = convertView;
		}

		try {
			paoPaoButton1 = (ImageView) view.findViewById(R.id.paoPaoButton1);
			child_is_login = (ImageView) view.findViewById(R.id.child_is_login);
			child_name = (TextView) view.findViewById(R.id.child_name);
			child_qianming = (TextView) view.findViewById(R.id.child_qianming);
		} catch (ClassCastException e) {
//			   
//					"You must supply a resource ID for a TextView");
			throw new IllegalStateException(
					"ArrayAdapter requires the resource ID to be a TextView", e);
		}
		ImageLoader.getInstance().displayImage(
				Constants.BadiDownImgUrl
						+ ((SortModel) mObjects.get(position)).getHead(),
				paoPaoButton1, options);
		child_name.setText(((SortModel) mObjects.get(position)).getNickName());
		//    
		// "打印出来的名字是:"
		// + ((SortModel) mObjects.get(position)).getNickName());
		child_qianming.setText(((SortModel) mObjects.get(position)).getMotto());
		if (((SortModel) mObjects.get(position)).getIs_login().equals("1")) {
			child_is_login
					.setImageResource(R.drawable.shape_blue_circular_bead);
		} else {
			child_is_login
					.setImageResource(R.drawable.shape_gray_circular_bead);
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg5) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, ChatActivity.class);
				intent.putExtra("id",
						((SortModel) mObjects.get(position)).getId());
				intent.putExtra("name",
						((SortModel) mObjects.get(position)).getNickName());
				intent.putExtra("head",
						((SortModel) mObjects.get(position)).getHead());
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				mContext.startActivity(intent);
			}
		});

		return view;
	}

	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new ArrayFilter();
		}
		return mFilter;
	}

	private class ArrayFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();

			if (mOriginalValues == null) {
				synchronized (mLock) {
					mOriginalValues = new ArrayList<T>(mObjects);//
				}
			}

			if (prefix == null || prefix.length() == 0) {
				synchronized (mLock) {
					// ArrayList<T> list = new ArrayList<T>();//无
					ArrayList<T> list = new ArrayList<T>(mOriginalValues);// List<T>
					results.values = list;
					results.count = list.size();
				}
			} else {
				String prefixString = prefix.toString().toLowerCase();
//				   

				final ArrayList<T> hanzi = mOriginalValues;// 汉字String
				final int count = hanzi.size();
//				   
//						+ count);
				final Set<T> newValues = new HashSet<T>(count);// 支持多音字,不重复

				for (int i = 0; i < count; i++) {
					final T value = hanzi.get(i);// 汉字String
					// final String valueText =
					// value.toString().toLowerCase();// 汉字String
					final String valueText = ((SortModel) value).getNickName()
							.toLowerCase();// 汉字String
					final Set<String> pinyinSet = pinyinList.get(i);// 支持多音字,类似:{z,c}
					Iterator iterator = pinyinSet.iterator();// 支持多音字
					while (iterator.hasNext()) {// 支持多音字
						final String pinyin = iterator.next().toString()
								.toLowerCase();// 取出多音字里的一个字母
						//    
						if (pinyin.indexOf(prefixString) != -1) {// 任意匹配
//							   
//									+ prefixString);
							newValues.add(value);
						} else if (valueText.indexOf(prefixString) != -1) {// 如果是汉字则直接添加
//							   
//									"添加的数据2是:"
//											+ valueText.indexOf(prefixString)
//											+ "  " + valueText + "   "
//											+ prefixString);
							newValues.add(value);
						}
					}
					if (maxMatch > 0) {// 有数量限制
						if (newValues.size() > maxMatch - 1) {// 不要太多
							break;
						}
					}

				}
				List<T> list = Set2List(newValues);// 转成List
				results.values = list;
				results.count = list.size();
			}
			return results;
		}

		protected void publishResults(CharSequence constraint,
				FilterResults results) {

			mObjects = (List<T>) results.values;
//			   
			if (results.count > 0) {
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}
		}
	}

	// List Set 相互转换
	public <T extends Object> Set<T> List2Set(List<T> tList) {
		Set<T> tSet = new HashSet<T>(tList);
		// TODO 具体实现看需求转换成不同的Set的子类。
		return tSet;
	}

	public <T extends Object> List<T> Set2List(Set<T> oSet) {
		List<T> tList = new ArrayList<T>(oSet);
		// TODO 需要在用到的时候另外写构造，根据需要生成List的对应子类。
		return tList;
	}
}
